from django.shortcuts import render
from django_redis import get_redis_connection
from rest_framework import mixins
from rest_framework.generics import ListAPIView,GenericAPIView,CreateAPIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.filters import OrderingFilter
from goods.models import GoodsCategory, SKU

from goods.serializers import SKUSSerializers, SKUIndexSerializer, SKUHistorySerializer

from drf_haystack.viewsets import HaystackViewSet
# Create your views here.



class CategoriesView(APIView):
    '''
        面包屑导航分类数据获取
    '''
    def get(self,request,pk):

        # 1,获取三级分类id
        # 2,获取三级分类对象
        cat3 = GoodsCategory.objects.get(id=pk)
        # 3,获取二级分类对象
        cat2 = cat3.parent
        # 4,查询一级分类对象
        cat1 = cat2.parent
        # 5,返回结果
        return Response({
            'cat1':cat1.name,
            'cat2':cat2.name,
            'cat3':cat3.name,
        })


class SKUSView(ListAPIView):
    '''
        获取当前分类多有商品
    '''

    #指定序列化器
    serializer_class = SKUSSerializers

    #指定查询集
    # queryset = SKU.objects.filter(category_id=pk)

    #指定分页
    # pagination_class = PageNum

    # 指定过滤排序
    filter_backends = (OrderingFilter,)
    ordering_fields = ('create_time','sales','price')

    def get_queryset(self):
        pk = self.kwargs['pk']
        return SKU.objects.filter(category_id=pk,is_launched=True)

    # def get(self,request, pk):
    #     '''
    #         获取当前分类所有商品
    #         请求路径:/categories/pk/skus/
    #     :param request: get
    #     :param pk: pk
    #     :return: __all__
    #     '''
    #     # 获取分类id
    #     # 查询当前分了类下所有商品
    #     # 返回序列化结果  分页和过滤处理


class SKUSearchViewSet(HaystackViewSet):
    """
    SKU搜索
    """
    index_models = [SKU]            #指定查询的模型类

    serializer_class = SKUIndexSerializer


class SKUHistoryView(CreateAPIView,ListAPIView):
    '''
        用户浏览历史记录
    '''
    # serializer_class = SKUHistorySerializer
    # permission_classes = [IsAuthenticated]
    pagination_class = None

    def get_queryset(self):
        # 获取用户
        user = self.request.user
        # 建立缓存连接对象
        coon = get_redis_connection('history')
        # 查询缓存数据获取到sku_id
        sku_ids = coon.lrange('history_%s'%user.id,0,100)
        # 根据sku_id查询商品对象
        skus = SKU.objects.filter(id__in=sku_ids)
        return skus

    def get_serializer_class(self):
        # 根据不同的请求返回不同的序列化器
        if self.request.method == 'POST':
            return SKUHistorySerializer
        else:
            return SKUSSerializers

    # def get(self,request):
    #
    #     user_id = request.user.id
    #
    #     redis_conn = get_redis_connection("history")
    #     history = redis_conn.lrange("history_%s" % user_id, 0, 100)
    #     skus = []
    #     # 为了保持查询出的顺序与用户的浏览历史保存顺序一致
    #     for sku_id in history:
    #         sku = SKU.objects.get(id=sku_id)
    #         skus.append(sku)
    #
    #     s = SKUSSerializers(skus, many=True)
    #     return Response(s.data)